<template>
  <a-modal style="top:5%;height:95%" :title="title" :width="modalWidth" :visible="visible"
  :confirmLoading="confirmLoading" cancelText="关闭" wrapClassName="ant-modal-cust-warp"
  @ok="handleSubmit" @cancel="handleCancel">

    <a-form :form="form" :label-col="labelCol" :wrapper-col="wrapperCol">
      <a-row :gutter="24">
        <a-col :span="24">
           <a-form-model-item label="代码生成目录" :labelCol="single ? labelCol: labelCol2" :wrapperCol="single ? wrapperCol: wrapperCol2">
              <!-- <a-input-search v-decorator="['projectPath', validatorRules.projectPath]"
                              @change="storeProjectPath" @search="onSelectDir" placeholder="请选择代码生成目录">
                <a-button slot="enterButton" icon="folder-open">浏览</a-button>
              </a-input-search> -->
              <a-input v-decorator="['projectPath', validatorRules.projectPath]"></a-input>
           </a-form-model-item>
        </a-col>

        <a-col :span="single ? 24 : 12">
          <a-form-model-item label="页面风格" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-select v-decorator="['jspMode']">
                <a-select-option v-for="(item,index) in jspModeList" :key="index" :value="item.code">
                  {{ item.note }}
                </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>

        <a-col :span="single ? 24 : 12">
          <a-form-model-item label="功能说明" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-decorator="['ftlDescription', validatorRules.ftlDescription]" placeholder="请输入功能说明"></a-input>
          </a-form-model-item>
        </a-col>

        <a-col :span="single ? 24 : 12" v-show="!1">
          <a-form-model-item label="数据模型" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-select v-decorator="['jformType']">
              <a-select-option value="1">单表</a-select-option>
              <a-select-option value="2">一对多</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>


        <a-col :span="single ? 24 : 12">
          <a-form-model-item label="表名" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-decorator="['tableName_tmp', validatorRules.tableName_tmp]" placeholder="请输入表名"></a-input>
          </a-form-model-item>
        </a-col>

        <a-col :span="single ? 24 : 12">
          <a-form-model-item label="实体类名" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-decorator="['entityName', validatorRules.entityName]" placeholder="请输入实体类名(首字母大写)"></a-input>
          </a-form-model-item>
        </a-col>

        <a-col :span="single ? 24 : 12">
          <a-form-model-item label="包名(小写)" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input v-decorator="['entityPackage', validatorRules.entityPackage]" placeholder="请输入包名"></a-input>
          </a-form-model-item>
        </a-col>

        <a-col :span="single ? 24 : 12">
          <a-form-model-item label="代码分层样式" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-select v-decorator="['packageStyle']" :disabled="true">
              <a-select-option value="service">业务分层</a-select-option>
              <a-select-option value="project">代码分层</a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
      </a-row>

      <a-card class="online-gen-subinfo" title="子表信息" v-if="subInfoList.length>0">
        <a-row  v-for="(item,index) in subInfoList" :key="'sub'+index">
            <a-col span="8">
              <a-form-model-item label="子表名" :labelCol="subLabelCol" :wrapperCol="subWrapperCol">
                <a-input v-decorator="['subList['+index+'].tableName', {initialValue:item.tableName,rules: [{required: true, message: '请输入子表名!'}]}]" placeholder="请输入子表名"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="子表实体" :labelCol="subLabelCol" :wrapperCol="subWrapperCol">
                <a-input v-decorator="['subList['+index+'].entityName', {initialValue:item.entityName,rules: [{required: true, message: '请输入子表实体!'}]}]" placeholder="请输入子表实体"></a-input>
              </a-form-model-item>
            </a-col>
            <a-col span="8">
              <a-form-model-item label="功能说明" :labelCol="subLabelCol" :wrapperCol="subWrapperCol">
                <a-input v-decorator="['subList['+index+'].ftlDescription', {initialValue:item.tableTxt,rules: [{required: true, message: '请输入功能说明!'}]}]" placeholder="请输入功能说明"></a-input>
              </a-form-model-item>
            </a-col>
        </a-row>
      </a-card>
    </a-form>
    <file-select-modal ref="fsm" @callback="updateFileDir"></file-select-modal>
  </a-modal>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import FileSelectModal from './FileSelectModal'
  import JCheckbox from '@/components/jeecg/JCheckbox'
  import ATextarea from 'ant-design-vue/es/input/TextArea'
  import ARow from 'ant-design-vue/es/grid/Row'
  import ACol from 'ant-design-vue/es/grid/Col'
  export default {
    name: "CodeGenerator",
    components: {
        ACol,
        ARow,
        ATextarea,
        FileSelectModal,
        JCheckbox
    },
    data: function() {
      return {
        modalWidth: 800,
        title: "代码生成",
        visible: !1,
        confirmLoading: !1,
        form: this.$form.createForm(this),
        subLabelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 8
          }
        },
        subWrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 15
          }
        },
        labelCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 6
          }
        },
        labelCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 3
          }
        },
        wrapperCol: {
          xs: {
            span: 24
          },
          sm: {
            span: 16
          }
        },
        wrapperCol2: {
          xs: {
            span: 24
          },
          sm: {
            span: 20
          }
        },
        genCodeTypes: [{
          label: "controller",
          value: "controller"
        },
          {
            label: "service",
            value: "service"
          },
          {
            label: "dao",
            value: "dao"
          },
          {
            label: "mapper.xml",
            value: "mapper"
          },
          {
            label: "entity",
            value: "entity"
          },
          {
            label: "vue",
            value: "vue"
          }],
        validatorRules: {
          projectPath: {
            rules: [{
              required: !0,
              message: "请选择代码生成目录!"
            }]
          },
          tableName_tmp: {
            rules: [{
              required: !0,
              message: "请输入表名!"
            }]
          },
          entityPackage: {
            rules: [{
              required: !0,
              message: "请输入包名!"
            }]
          },
          entityName: {
            rules: [{
              required: !0,
              message: "请输入实体类名!"
            }]
          }
        },
        url: {
          tableInfo: "/online/cgform/head/tableInfo",
          codeGenerate: "/online/cgform/special/api/codeGenerate"
        },
        code: "",
        subInfoList: [],
        single: !1,
        metaModel: {
          projectPath: "",
          packageStyle: "service",
          jspMode: "",
          jformType: "1",
          tableName_tmp: "",
          ftlDescription: "",
          entityName: "",
          codeTypes: "controller,service,dao,mapper,entity,vue"
        },
        model: {},
        jspModeList: []
      }
    },
    methods: {
      async handleSubmit(){
        var e = this;
        this.form.validateFields((function(t, n) {
          if (!t) {
            e.confirmLoading = !0;
            var r = Object.assign({},
              n, {
                code: e.code,
                tableName: n.tableName_tmp
              });
            httpAction(e.url.codeGenerate, r).then((function(t) {
              t.success ? (e.$message.success("代码生成成功!"), e.handleCancel()) : e.$message.warning(t.message),
                e.confirmLoading = !1
            }))
          }
        }))
      },
      handleCancel() {
        this.visible = !1
      },
      show(e) {
          var t = this;
          this.confirmLoading = !1,
          this.visible = !0,
          this.getStoreProjectPath(),
          this.code = e,
          this.subInfoList = [],
          this.model = Object.assign({},this.metaModel),
          this.modalWidth = 800,

          httpAction(this.url.tableInfo, {
            code: e
          },'post').then((function(e) {
            if (e.success) {
              var n = e.result.main;
              t.jspModeList = e.result.jspModeList,
              //t.jspModeList = Object(i["a"])(e.result.jspModeList),
                "Y" == n.isTree ? t.model.jspMode = "tree": t.model.jspMode = t.jspModeList[0].code,
                t.single = 1 == n.tableType,
                t.title = "代码生成【" + n.tableName + "】",
                (t.model.projectPath = e.result.projectPath, window.localStorage.setItem('jeecg_onl_project_path', e.result.projectPath)),
                t.model.jformType = n.tableType + "",
                t.model.tableName_tmp = n.tableName,
                t.model.ftlDescription = n.tableTxt;
              var r = n.tableName;
              t.model.entityName = r.substring(0, 1).toUpperCase() + r.substring(1),
                t.$nextTick((function() {
                  t.form.setFieldsValue(t.model)
                })),
              e.result.sub && e.result.sub.length > 0 && (t.subInfoList = e.result.sub, t.modalWidth = 1200)
            } else t.$message.warning("表信息加载失败")
          }))
      },
      onSelectDir() {
        this.$refs.fsm.show()
      },
      updateFileDir(e) {
        this.form.setFieldsValue({
          projectPath: e
        })
      },
      getCamelCase(e) {
        var t = e;
        return t.substring(0, 1).toUpperCase() + t.substring(1)
      },
      storeProjectPath(e) {
        e.target.value && window.localStorage.setItem('jeecg_onl_project_path', e.target.value)
      },
      getStoreProjectPath() {
        var e = window.localStorage.getItem('jeecg_onl_project_path');
        e && (this.metaModel.projectPath = e)
      }
    }
  }
</script>